package com.mall.mymall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.mall.mymall.dao.dto.QueryDto;
import com.mall.mymall.dao.dto.query.OrganizationQuery;
import com.mall.mymall.dao.dto.querypage.OrganizationPageRequest;
import com.mall.mymall.dao.entity.Organizations;
import com.mall.mymall.mapper.OrganizationsMapper;
import com.mall.mymall.service.OrganizationsService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrganizationsServiceImpl extends ServiceImpl<OrganizationsMapper, Organizations> implements OrganizationsService {

    /**
     * 根据条件分页查询组织信息
     *
     * @param organizationPageRequest 包含分页和查询条件的请求对象
     * @return 返回包含分页信息和组织记录的查询结果对象
     */
    public QueryDto<Organizations> pageOrganizations(OrganizationPageRequest organizationPageRequest) {
        // 创建分页对象
        Page<Organizations> page = new Page<>(organizationPageRequest.getPageRequestDto().getPage(), organizationPageRequest.getPageRequestDto().getSize());

        // 创建条件构造器
        QueryWrapper<Organizations> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .like(organizationPageRequest.getQuery().getOrganizationId() != null, "organization_id", organizationPageRequest.getQuery().getOrganizationId())
                .like(organizationPageRequest.getQuery().getCompanyName() != null, "company_name", organizationPageRequest.getQuery().getCompanyName())
                .like(organizationPageRequest.getQuery().getCompanyAddress() != null, "company_address", organizationPageRequest.getQuery().getCompanyAddress());

        // 执行分页查询
        IPage<Organizations> resultPage = this.page(page, queryWrapper);

        // 构造返回结果
        QueryDto<Organizations> result = new QueryDto<>();
        result.setTotal((int) resultPage.getTotal());
        result.setRecords(resultPage.getRecords());

        return result;
    }



}
